package org.wisigoth.chat.client.jabber;

import java.util.LinkedList;

/**
 * A simple class that queues up packets for processing, and processes them.
 */
public class InPacketQueue {
	LinkedList<Packet> queue = new LinkedList<Packet>();

	/**
	 * Push a packet onto the end of the queue.
	 */
	public synchronized void push(Packet packet) {
		//System.out.println("[PQ] " + packet);
		this.queue.add(packet);
		this.notifyAll();
	}

	/**
	 * Pull the packet at the head of the queue.
	 * 
	 * @return The packet at the head of the queue, or null if no packets are
	 *         available
	 */
	public synchronized Packet pull() {
		try {
			while (this.queue.isEmpty()) {
				this.wait();
			}
		} catch (InterruptedException e) {
			return null;
		}
		return this.queue.remove(0);
	}
}